package com.kaili.basic.controller.web.v1;

import com.baomidou.mybatisplus.plugins.Page;
import com.kaili.basic.pojo.dto.*;
import com.kaili.basic.pojo.entity.BaseAllottedStoreEntity;
import com.kaili.basic.pojo.vo.BaseAllottedStoreDetailVO;
import com.kaili.basic.pojo.vo.BaseAllottedStoreExportVO;
import com.kaili.basic.service.BaseAllottedStoreService;
import com.kaili.basic.version.ApiVersion;
import com.kaili.common.exception.RestException;
import com.kaili.common.response.CommonStatus;
import com.kaili.common.response.RestStatus;
import com.kaili.common.util.ExcelUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import javax.servlet.http.HttpServletResponse;
import javax.transaction.Transactional;
import java.util.HashMap;
import java.util.List;
import java.util.UUID;

@ApiVersion(1)
@RestController
@RequestMapping("/{api_version}")
@Transactional
@Api(tags = "调拨管理", description = "调拨管理")
public class BaseAllottedStoreController {
    @Autowired
    private BaseAllottedStoreService baseAllottedStoreService;

    @GetMapping("/allottedStore/code")
    @ApiOperation(value = "调拨单号", notes = "调拨单号")
    public Long queryStoreOrderNo(){
        return baseAllottedStoreService.queryStoreOrderNo();
    }

    @GetMapping("/allottedStore")
    @ApiOperation(value = "查询调拨单", notes = "查询调拨单")
    public Page<BaseAllottedStoreDetailVO> queryPageList(@ModelAttribute BaseAllottedStoreQueryDTO queryDto){
        return baseAllottedStoreService.queryPageList(new Page<BaseAllottedStoreDetailVO>(queryDto.getPage(),queryDto.getPageSize()),queryDto);
    }

    @GetMapping("/allottedStore/detail/{orderNo}")
    @ApiOperation(value = "查询调拨单明细")
    List<BaseAllottedStoreDetailVO> queryDetailByOrderNo(@ApiParam("调拨单号") @PathVariable("orderNo")final String orderNo){
        return baseAllottedStoreService.queryDetailByOrderNo(new HashMap<String,Object>(){{put("orderNo",orderNo);}});
    }

    @GetMapping("/allottedStore/edit/{orderNo}")
    @ApiOperation(value = "查询调拨单明细")
    BaseAllottedStoreDetailVO queryAllottedStoreDetail(@ApiParam("调拨单号") @PathVariable("orderNo")final String orderNo){
        return baseAllottedStoreService.queryAllottedStoreDetail(new HashMap<String,Object>(){{put("orderNo",orderNo);}});
    }

    @PostMapping("/allottedStore")
    @ApiOperation(value = "新增调拨")
    public RestStatus addAllottedStore(@RequestBody BaseAllottedStoreAddDTO addDto){
        return baseAllottedStoreService.addAllottedStore(addDto);
    }

    @PutMapping("/allottedStore")
    @ApiOperation(value = "修改调拨")
    public RestStatus updateAllottedStore(@RequestBody BaseAllottedStoreEditDTO editDTO){
        return baseAllottedStoreService.updateAllottedStore(editDTO);
    }

    @PutMapping("/allottedStore/audit")
    @ApiOperation(value = "审核移库单", notes = "审核移库单")
    public RestStatus auditAllottedStore(@RequestBody BaseAllottedStoreAuditDto auditDto)throws Exception{
        return baseAllottedStoreService.auditAllottedStore(auditDto);
    }

    @DeleteMapping("/allottedStore/{id}")
    @ApiOperation(value = "删除调拨")
    public RestStatus deleteAllottedStore(@ApiParam("订单号id")@PathVariable("id") long id){
        BaseAllottedStoreEntity entity=new BaseAllottedStoreEntity();
        entity.setId(id);
        entity.setStatus("0");
        baseAllottedStoreService.updateById(entity);
        return CommonStatus.DELETE_OK;
    }

    @GetMapping("/allottedStore/export")
    @ApiOperation(value = "调拨明细导出")
    public void allottedStoreExport(HttpServletResponse response, @ModelAttribute BaseAllottedStoreQueryDTO queryDto) throws RestException {
        List<BaseAllottedStoreExportVO> list = baseAllottedStoreService.allottedStoreExport(queryDto);
        ExcelUtil.exportExcel(list,"出库管理","sheet1", BaseAllottedStoreExportVO.class, UUID.randomUUID().toString()+".xls",response);
    }
}


